package com.example.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class MainGamePage extends View {
	
	/* page information */
	private int pageLeft, pageRight, pageTop, pageBottom;
	private int pageHeight, pageWidth;
	
	/* chess board information */
	private int boardLeft, boardRight, boardTop, boardBottom;
	private int boardWidth;
	private int littleRectWidth;
	
	/* three squares in chess board */
	private Rect littleRect, middleRect, bigRect;

	public MainGamePage(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	protected void onLayout(boolean changed, int left, int top, int right, int bottom)
	{
		// TODO Auto-generated method stub
		super.onLayout(changed, left, top, right, bottom);
		
		pageLeft = left;
		pageRight = right;
		pageTop = top;
		pageBottom = bottom;
		
		pageHeight = bottom - top;
		pageWidth = right - left;
		
		boardLeft = pageLeft;
		boardRight = pageRight;
		boardWidth = boardRight - boardLeft;
		boardBottom = pageBottom;
		boardTop = boardBottom - boardWidth;
		littleRectWidth = boardWidth/5;
		
		//littleRectWidth = ((pageHeight > pageWidth) ? pageWidth/5: pageHeight/5);
	}
	
	protected void onDraw(Canvas canvas)
	{
		initChessBoard(canvas);
	}
	
	private void initChessBoard(Canvas canvas)
	{
		Paint paint = new Paint();
		
		paint.setAntiAlias(true);
		paint.setColor(Color.BLACK);
		paint.setStyle(Style.STROKE);
		canvas.drawColor(Color.WHITE);
		
		littleRect = new Rect(
				(boardLeft + boardRight)/2 - littleRectWidth/2,
				(boardTop + boardBottom)/2 - littleRectWidth/2,
				(boardLeft + boardRight)/2 + littleRectWidth/2,
				(boardTop + boardBottom)/2 + littleRectWidth/2
				);
		middleRect = new Rect(
				littleRect.left - littleRectWidth,
				littleRect.top - littleRectWidth,
				littleRect.right + littleRectWidth,
				littleRect.bottom + littleRectWidth
				);
		bigRect = new Rect(
				littleRect.left - 2*littleRectWidth,
				littleRect.top - 2*littleRectWidth,
				littleRect.right + 2*littleRectWidth,
				littleRect.bottom + 2*littleRectWidth
				);
		canvas.drawRect(littleRect, paint);
		canvas.drawRect(middleRect, paint);
		canvas.drawRect(bigRect, paint);
	}
}
